package com.yanqu.road.dao.impl.activity.tonbing;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tonbing.data.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTonBingDataMainDaoImpl extends TempDataObjectDao<CrossTonBingDataMain> {

    public static String getSql = "select * from `t_c_tonbing_data_main` where `activityId`=?";

    public CrossTonBingDataMain getCrossTonBingDataMain(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateSql = "update `t_c_tonbing_data_main` set lockUnion=?,turnUnionMap=?,winnerMap=? where activityId=?";

    @Override
    protected boolean updateData(CrossTonBingDataMain crossTonBingData) {
        crossTonBingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, crossTonBingData.isLockUnion()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingData.getTurnUnionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingData.getWinnerMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingData.getActivityId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSql = "insert into `t_c_tonbing_data_main`(" +
            "activityId,lockUnion,turnUnionMap,winnerMap) values (?,?,?,?)";

    @Override
    protected boolean addData(CrossTonBingDataMain data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isLockUnion()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getTurnUnionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getWinnerMap())));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossTonBingDataMain parseData(ResultSet rs) throws SQLException {
        CrossTonBingDataMain data = new CrossTonBingDataMain();
        data.setActivityId(rs.getInt("activityId"));
        data.setLockUnion(rs.getBoolean("lockUnion"));
        String str = rs.getString("turnUnionMap");
        if(!StringUtils.isNullOrEmpty(str)){
            data.setTurnUnionMap(JSON.parseObject(str, new TypeReference<ConcurrentHashMap<Integer, Map<Integer, Map<Integer, List<String>>>>>() {
            }));
        }
        str = rs.getString("winnerMap");
        if(!StringUtils.isNullOrEmpty(str)){
            data.setWinnerMap(JSON.parseObject(str, new TypeReference<ConcurrentHashMap<Integer,Long>>() {
            }));
        }
        return data;
    }

}
